//
//  VisItorLoginView.swift
//  swift微博项目
//
//  Created by 王明 on 15/11/9.
//  Copyright © 2015年 王明. All rights reserved.
//

import UIKit

protocol VisItorLoginViewDelegate: NSObjectProtocol
{

    //协议的登录和注册
    func VisItorWillLogin()
    
    func VisItorWillRegister()


}

class VisItorLoginView: UIView {
    //声明代理属性
    //属性默认是强引用  需要添加weak
    weak var VisItorDelegate: VisItorLoginViewDelegate?
    
    @objc func loginDidBtnClick(){
    //代理调用协议方法
        VisItorDelegate?.VisItorWillLogin()
    
    }
    @objc func registerBtnDidClick(){
        //代理调用协议方法
        VisItorDelegate?.VisItorWillRegister()
        
    }
    
    //MARK: 懒加载所有的控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLable : UILabel = {
    
        let l = UILabel()
        l.text = "关注一些人，回来看看也许有什么惊喜"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14)
        l.textColor = UIColor.lightGrayColor()
        l.numberOfLines = 0
        return l
    
    }()
    
    //设置页面信息
    func setUIInfo(imageName : String? , title : String){
    
        iconView.hidden = false
        tipLable.text = title
        if imageName != nil{
        
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        
        }else{
        //让circleView动画动起来
            startAnimation()
        
        }
        
    }
    
    //重写构造方法
    init (){
    
        super.init(frame: CGRectZero)
        setupUI()
//        startAnimation()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func startAnimation(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        //当动画结束 或者视图处于非活跃状态  动画不移除图层
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    private func setupUI(){
    //添加各种控件视图
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置frame失效
        for v in subviews {
           v.translatesAutoresizingMaskIntoConstraints = false
        
        }
        
        
        //设置布局
        //注意：布局的约束尽量相对于一个控件
        
        //圆圈的约束
        print(self)
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //大头标的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //设置文案的约束
         addConstraint(NSLayoutConstraint(item: tipLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
        
        //设置宽度约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //设置登陆按钮的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLable, attribute: .Left, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        //按钮的宽高的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
         addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))

        
        //设置注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLable, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        
        
        //设置背景视图的约束
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-15)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)

    }
    
    
    
   private lazy var loginBtn: UIButton = {
    let btn = UIButton()
    
    btn.setTitle("登陆", forState: UIControlState.Normal)
    btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
    btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    
    return btn
    }()
    
    private lazy var registerBtn:UIButton = {
    
    let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
    return btn
    
    }()
    
    
   
    
    func VisItorWillLogin(){
    print("ni")
    
    }
    func VisItorWillRegister(){
    print("我")
    
    }
    
    
    

    
}
